local notifications = {};
local maxNotifications = 3;
local isDraw = false;
local _idx = 0;
local deltaTime = 36;

--PRIVATE FUNCTIONS
function _isNotificationsEmpty()
	return (next(notifications) == nil);
end

function _checkNotifications()
	if (_isNotificationsEmpty()) then
		removeEventHandler("onClientPreRender", root, preDraw);
		removeEventHandler("onClientRender", root, draw);
		isDraw = false;
	end
end

function _tableFind(t, idx)
	for i, v in pairs(t) do
		if v["idx"] == idx then
			return i;
		end
	end
end

function _remove(idx)
	if (not _isNotificationsEmpty()) then
		table.remove(notifications, _tableFind(notifications, idx));
		_checkNotifications();
	end
end

function _fromColor(color)
	if color then
		local blue = bitExtract(color, 0, 8);
		local green = bitExtract(color, 8, 8);
		local red = bitExtract(color, 16, 8);
		local alpha = bitExtract(color, 24, 8);
		
		return { red, green, blue, alpha };
	end
end

function _currentAlpha(color, absValue)
	max = max or 255;
	local r, g, b, a = unpack(_fromColor(color));
	return tocolor(r, g, b, absValue * a);
end

--EXPORTED FUNCTIONS
function add(_text, _delay, _fade, _textColor, _bgColor,_idnot)
	_textColor = _textColor or tocolor(255, 255, 255);
	_bgColor = _bgColor or tocolor(0, 0, 0, 155);
	_delay = _delay or 7000;
	_fade = _fade or 220;
	_idnot = _idnot or 1;
	tick = getTickCount()
	_currentIdx = _idx;
	_idx = _idx + 1;

	table.insert(notifications, {
		idx = _currentIdx,
		text = _text,
		textColor = _textColor,
		bgColor = _bgColor,
		IDnot = _idnot,
		delay = _delay,
		fade = _fade,
		opacity = 1
	});

	if not isDraw then
		addEventHandler("onClientPreRender", root, preDraw);
		addEventHandler("onClientRender", root, draw);
	end
	
	return _currentIdx;
end

addEvent("addServer", true);
addEventHandler("addServer", localPlayer, add);

function shift()
	if (not _isNotificationsEmpty()) then
		table.remove(notifications, 1);
		_checkNotifications();
	end
end

addEvent("shiftServer", true);
addEventHandler("shiftServer", localPlayer, shift);

function pop()
	if (not _isNotificationsEmpty()) then
		table.remove(notifications, #notifications);
		_checkNotifications();
	end
end

addEvent("popServer", true);
addEventHandler("popServer", localPlayer, pop);

function remove(idx, instant)
	if (instant) then
		_remove(idx);
	else
		local foundIdx = _tableFind(notifications, idx);

		if foundIdx then
			notifications[foundIdx]["delay"] = 0;
		end
	end
end

addEvent("removeServer", true);
addEventHandler("removeServer", localPlayer, remove);

function preDraw(timeSlice)
	deltaTime = timeSlice;
end

local screenW, screenH = guiGetScreenSize()
local sx, sy = guiGetScreenSize()

function scale_x(value)
    local result = (value / 1360) * sx

    return result
end

function scale_y(value)
    local result = (value / 768) * sy

    return result
end


loadstring(exports.dgs:dgsImportFunction())()
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", scale_x(10) )


function draw()
	isDraw = true;
	for i, notification in ipairs(notifications) do
		if notification["IDnot"] == 1 then
		if i > 3 then return end
		else
		if i > 1 then return end
		end
	local dodatekY = (scale_y(60))*(i-1)
    local dodatekY2 = (scale_y(120))*(i-1)
        if (notification["opacity"] > 0.003921) then -- 1/255
			if notification["IDnot"] == 1 then
			    dxDrawRectangle(scale_x(1042), scale_y(100) + dodatekY, scale_x(305), scale_y(40), _currentAlpha(notification["bgColor"], notification["opacity"]), false);
			    dxDrawText(notification["text"], scale_x(1042), scale_y(42) + dodatekY2, scale_x(1345), scale_y(199), _currentAlpha(notification["textColor"], notification["opacity"]), 1, font, "center", "center", false, true, false, false, false);
            else
				dxDrawRectangle(scale_x(0), scale_y(0) + dodatekY, scale_x(1360), scale_y(40), _currentAlpha(notification["bgColor"], notification["opacity"]), false);
				dxDrawText(notification["text"], scale_x(0), scale_y(0) + dodatekY2, scale_x(1360), scale_y(40), _currentAlpha(notification["textColor"], notification["opacity"]), 1, font, "center", "center", false, true, false, false, false);
			end
			if(notification["delay"] > 0) then
				notification["delay"] = notification["delay"] - deltaTime;
			else
				notification["opacity"] = (notification["opacity"] / notification["fade"]) * (notification["fade"] - deltaTime);
			end
		else
			_remove(notification["idx"]);
		end
	end
end